package com.wlz.rocketmq.simple;

import com.wlz.rocketmq.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 *  同步发送消息、 单项发送
 * @author wlz
 * @date 2022-07-01  9:29 下午
 */
@Slf4j
public class SyncProducer {

    public static void main(String[] args) throws Exception{

        DefaultMQProducer producer = new DefaultMQProducer("productGroupName");

        producer.setNamesrvAddr(Constants.NAMESVADDR);

        producer.start();

        for (int i = 0; i < 20; i++) {
            try {
                Message msg = new Message("TopicTest", "TagA", "OrderID188",
                        "hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                //同步传递消息，消息会发给集群中的一个Broker节点。
                    SendResult sendResult = producer.send(msg);
                log.info(" sendResult {}", sendResult);
                // 单项发送
//                producer.sendOneway(msg);
            } catch (Exception e) {
              log.info("SyncProducer error : {}", e);
            }
        }

        producer.shutdown();


    }
}
